package ManHinhChinh;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Monitor;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.wb.swt.SWTResourceManager;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;

import java.sql.Statement;

import ManHinhChinh.NhaCungCap;
import ManHinhChinh.ThemKhachHang;
import ManHinhChinh.TimKiemKhachHang;
import ManHinhChinh.ThemHangHoa;
import ManHinhChinh.TaoKetNoi;
import ManHinhChinh.NhaCungCap;

import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.events.FocusAdapter;
import org.eclipse.swt.events.FocusEvent;
import org.eclipse.swt.events.ShellAdapter;
import org.eclipse.swt.events.ShellEvent;
import org.eclipse.swt.graphics.Rectangle;
public class Menu extends DangNhap {

	protected Shell Menu;

	/**
	 * Launch the application.
	 * @param args
	 */
	DangNhap dn = new DangNhap();
	private Table MHC_Bang;
	

	/**
	 * Open the window.
	 */
	public void open() {
		Display display = Display.getDefault();
		createContents();
		
		// canh giua man hinh
				Monitor primary = display.getPrimaryMonitor();
				Rectangle bounds = primary.getBounds();
				Rectangle rect = Menu.getBounds();
				int x = bounds.x + (bounds.width - rect.width) / 2;
				int y = bounds.y + (bounds.height - rect.height) / 2;
				Menu.setLocation(x, y);
		Menu.open();
		Menu.layout();	
		while (!Menu.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
		
	}

	/**
	 * Create contents of the window.
	 * @wbp.parser.entryPoint
	 */
	protected void createContents() {
		Menu = new Shell(SWT.TITLE);
		Menu.addShellListener(new ShellAdapter() {
			@Override
			public void shellActivated(ShellEvent e) {
				capnhapTable();
			}
		});
		Menu.setImage(SWTResourceManager.getImage("..\\_QuanLyBanHang\\image\\generic_messenger.ico"));
		Menu.setSize(789, 542);
		Menu.setText("Qu\u1EA3n L\u00FD B\u00E1n H\u00E0ng");
		org.eclipse.swt.widgets.Menu menu = new org.eclipse.swt.widgets.Menu(Menu, SWT.BAR);
		Menu.setMenuBar(menu);
		
		MenuItem mnHeThong = new MenuItem(menu, SWT.CASCADE);
		mnHeThong.setImage(SWTResourceManager.getImage("..\\_QuanLyBanHang\\image\\HDD.ico"));
		mnHeThong.setText("H\u1EC7 Th\u1ED1ng");
		
		org.eclipse.swt.widgets.Menu mnHeThong_Exit = new org.eclipse.swt.widgets.Menu(mnHeThong);
		mnHeThong.setMenu(mnHeThong_Exit);
		
		new MenuItem(mnHeThong_Exit, SWT.SEPARATOR);
		
		MenuItem mntmThayDoiquydinh = new MenuItem(mnHeThong_Exit, SWT.NONE);
		mntmThayDoiquydinh.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				ThayDoiQuyDinh form = new ThayDoiQuyDinh();
				form.open();
			}
		});
		mntmThayDoiquydinh.setText("Thay đổi quy định");
		
		MenuItem mntmNewItem = new MenuItem(mnHeThong_Exit, SWT.NONE);
		mntmNewItem.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				System.exit(0);
			}
		});
		mntmNewItem.setImage(SWTResourceManager.getImage("..\\_QuanLyBanHang\\image\\exit.ico"));
		mntmNewItem.setText("Tho\u00E1t");
		
		MenuItem mnNghiepVu = new MenuItem(menu, SWT.CASCADE);
		mnNghiepVu.setImage(SWTResourceManager.getImage("..\\_QuanLyBanHang\\image\\d2.ico"));
		mnNghiepVu.setText("Nghi\u1EC7p V\u1EE5");
		
		org.eclipse.swt.widgets.Menu menu_4 = new org.eclipse.swt.widgets.Menu(mnNghiepVu);
		mnNghiepVu.setMenu(menu_4);
		
		MenuItem mnDanhMuc_NhapHang = new MenuItem(menu_4, SWT.NONE);
		mnDanhMuc_NhapHang.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				NhapHang nh = new NhapHang();
				nh.open();
			}
		});
		mnDanhMuc_NhapHang.setImage(SWTResourceManager.getImage("..\\_QuanLyBanHang\\image\\nh\u1EADp h\u00E0ng.png"));
		mnDanhMuc_NhapHang.setText("Nh\u1EADp H\u00E0ng");
		
		MenuItem mnNVuDatHang = new MenuItem(menu_4, SWT.CASCADE);
		mnNVuDatHang.setImage(SWTResourceManager.getImage("..\\_QuanLyBanHang\\image\\window plus.ico"));
		mnNVuDatHang.setText("\u0110\u1EB7t H\u00E0ng");
		
		org.eclipse.swt.widgets.Menu menu_7 = new org.eclipse.swt.widgets.Menu(mnNVuDatHang);
		mnNVuDatHang.setMenu(menu_7);
		
		MenuItem mnDatHang = new MenuItem(menu_7, SWT.NONE);
		mnDatHang.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				DatHang dh = new DatHang();
				dh.open();
			}
		});
		mnDatHang.setText("Phi\u1EBFu \u0110\u1EB7t H\u00E0ng");
		
		MenuItem mnDanhMuc_XuatHang = new MenuItem(menu_4, SWT.NONE);
		mnDanhMuc_XuatHang.setText("Giao hàng");
		mnDanhMuc_XuatHang.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				PhieuGiaoHang pgh = new PhieuGiaoHang();
				pgh.open();
			}
		});
		mnDanhMuc_XuatHang.setImage(SWTResourceManager.getImage("..\\_QuanLyBanHang\\image\\Xu\u1EA5t h\u00E0ng.png"));
		
		MenuItem mnNghiepVu_HDBL = new MenuItem(menu_4, SWT.NONE);
		mnNghiepVu_HDBL.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				HoaDonBanLe hd= new HoaDonBanLe();
				hd.open();
			}
		});
		mnNghiepVu_HDBL.setText("H\u00F3a \u0110\u01A1n B\u00E1n L\u1EBB");
		
		MenuItem mnDanhMuc = new MenuItem(menu, SWT.CASCADE);
		mnDanhMuc.setImage(SWTResourceManager.getImage("..\\_QuanLyBanHang\\image\\Folder-Open.ico"));
		mnDanhMuc.setText("Danh M\u1EE5c");
		
		org.eclipse.swt.widgets.Menu menu_3 = new org.eclipse.swt.widgets.Menu(mnDanhMuc);
		mnDanhMuc.setMenu(menu_3);
		
		MenuItem mnDanhMuc_MatHang = new MenuItem(menu_3, SWT.CASCADE);
		mnDanhMuc_MatHang.setImage(SWTResourceManager.getImage("..\\_QuanLyBanHang\\image\\MatHang.jpg"));
		mnDanhMuc_MatHang.setText("M\u1EB7t H\u00E0ng");
		
		org.eclipse.swt.widgets.Menu menu_9 = new org.eclipse.swt.widgets.Menu(mnDanhMuc_MatHang);
		mnDanhMuc_MatHang.setMenu(menu_9);
		
		MenuItem mnDM_MH_ThemMatHang = new MenuItem(menu_9, SWT.NONE);
		mnDM_MH_ThemMatHang.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				ThemHangHoa t = new ThemHangHoa();
				t.open();
			}
		});
		mnDM_MH_ThemMatHang.setText("Th\u00EAm M\u1EB7t H\u00E0ng");
		
		MenuItem mnDM_NH_TimMatHang = new MenuItem(menu_9, SWT.NONE);
		mnDM_NH_TimMatHang.setText("T\u00ECm M\u1EB7t H\u00E0ng");
		
		MenuItem mnDanhMucNCC = new MenuItem(menu_3, SWT.CASCADE);
		mnDanhMucNCC.setText("Nh\u00E0 Cung C\u1EA5p");
		
		org.eclipse.swt.widgets.Menu menu_10 = new org.eclipse.swt.widgets.Menu(mnDanhMucNCC);
		mnDanhMucNCC.setMenu(menu_10);
		
		MenuItem mnDM_NCC_ThemNCC = new MenuItem(menu_10, SWT.NONE);
		mnDM_NCC_ThemNCC.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				NhaCungCap ncc = new NhaCungCap();
				ncc.open();
			}
		});
		mnDM_NCC_ThemNCC.setText("Th\u00EAm Nh\u00E0 Cung C\u1EA5p");
		
		MenuItem mntmNewItem_2 = new MenuItem(menu_3, SWT.NONE);
		mntmNewItem_2.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				PhieuChiTien pct = new PhieuChiTien();
				pct.open();
			}
		});
		mntmNewItem_2.setText("Phi\u1EBFu Chi Ti\u1EC1n");
		
		MenuItem mntmNewItem_3 = new MenuItem(menu_3, SWT.NONE);
		mntmNewItem_3.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				PhieuThuTien ptt = new PhieuThuTien();
				ptt.open();
			}
		});
		mntmNewItem_3.setText("Phi\u1EBFu Thu Ti\u1EC1n");
		
		MenuItem mnBaoCao = new MenuItem(menu, SWT.CASCADE);
		mnBaoCao.setImage(SWTResourceManager.getImage("..\\_QuanLyBanHang\\image\\Thongke.ico"));
		mnBaoCao.setText("B\u00E1o C\u00E1o");
		
		org.eclipse.swt.widgets.Menu menu_5 = new org.eclipse.swt.widgets.Menu(mnBaoCao);
		mnBaoCao.setMenu(menu_5);
		
		MenuItem menuItem = new MenuItem(menu_5, SWT.SEPARATOR);
		
		MenuItem mnDanhMuc_TonKho = new MenuItem(menu_5, SWT.NONE);
		mnDanhMuc_TonKho.setImage(SWTResourceManager.getImage("..\\_QuanLyBanHang\\image\\t\u1ED3n kho.jpg"));
		mnDanhMuc_TonKho.setText("T\u1ED3n Kho");
		
		MenuItem menuItem_1 = new MenuItem(menu_5, SWT.SEPARATOR);
		
		MenuItem mnDanhMuc_DoanhThu = new MenuItem(menu_5, SWT.NONE);
		mnDanhMuc_DoanhThu.setImage(SWTResourceManager.getImage("..\\_QuanLyBanHang\\image\\Doanh Thu.png"));
		mnDanhMuc_DoanhThu.setText("Doanh Thu");
		
		MenuItem mntmNewItem_4 = new MenuItem(menu_5, SWT.NONE);
		mntmNewItem_4.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				ChiTietCongNo ctcn = new ChiTietCongNo();
				ctcn.open();
			}
		});
		mntmNewItem_4.setText("Chi Ti\u1EBFt C\u00F4ng N\u1EE3");
		
		MenuItem mntmNewItem_5 = new MenuItem(menu_5, SWT.NONE);
		mntmNewItem_5.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				TongHopCongNo thcn = new TongHopCongNo();
				thcn.open();
			}
		});
		mntmNewItem_5.setText("T\u1ED5ng H\u1EE3p C\u00F4ng N\u1EE3");
		
		MenuItem mntmNewItem_6 = new MenuItem(menu_5, SWT.NONE);
		mntmNewItem_6.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				TongHopTonKhoSanPham th = new TongHopTonKhoSanPham();
				th.open();
			}
		});
		mntmNewItem_6.setText("T\u1ED5ng H\u1EE3p T\u1ED3n Kho S\u1EA3n Ph\u1EA9m");
		
		MenuItem mntmNewItem_7 = new MenuItem(menu_5, SWT.NONE);
		mntmNewItem_7.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				
			}
		});
		mntmNewItem_7.setText("T\u1ED5ng H\u1EE3p T\u1ED3n Kho S\u1EA3n Ph\u1EA9m");
		
		MenuItem mnKhachHang = new MenuItem(menu, SWT.CASCADE);
		mnKhachHang.setImage(SWTResourceManager.getImage("..\\_QuanLyBanHang\\image\\H\u00E0ng H\u00F3a.jpg"));
		mnKhachHang.setText("Kh\u00E1ch H\u00E0ng");
		
		org.eclipse.swt.widgets.Menu menu_8 = new org.eclipse.swt.widgets.Menu(mnKhachHang);
		mnKhachHang.setMenu(menu_8);
		
		MenuItem mbKH_ThemKhachHang = new MenuItem(menu_8, SWT.NONE);
		mbKH_ThemKhachHang.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				ThemKhachHang tkh = new ThemKhachHang();
				tkh.open();
			}
		});
		mbKH_ThemKhachHang.setImage(SWTResourceManager.getImage("..\\_QuanLyBanHang\\image\\ADD.ICO"));
		mbKH_ThemKhachHang.setText("Th\u00EAm Kh\u00E1ch H\u00E0ng");
		
		MenuItem mvKH_SuaThongTin = new MenuItem(menu_8, SWT.NONE);
		mvKH_SuaThongTin.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				TimKiemKhachHang se = new TimKiemKhachHang();
				se.open();
			}
		});
		mvKH_SuaThongTin.setImage(SWTResourceManager.getImage("..\\_QuanLyBanHang\\image\\PrintPreview.ico"));
		mvKH_SuaThongTin.setText("T\u00ECm Ki\u1EBFm Kh\u00E1ch H\u00E0ng");
		
		MHC_Bang = new Table(Menu, SWT.BORDER | SWT.FULL_SELECTION | SWT.VIRTUAL);
		MHC_Bang.setBackground(SWTResourceManager.getColor(SWT.COLOR_WIDGET_LIGHT_SHADOW));
		MHC_Bang.setBounds(52, 68, 679, 382);
		MHC_Bang.setHeaderVisible(true);
		MHC_Bang.setLinesVisible(true);
		MHC_Bang.setItemCount(1);
		
//		table.addListener(SWT.SetData, new Listener() {
//			
//			@Override
//			public void handleEvent(Event event) {
//				// TODO Auto-generated method stub
//				table.setItemCount(0); 
//				TaoKetNoi s = new TaoKetNoi();
//				 Connection con = s.TaoLienKet();
//				 try {
//				 Statement stm = con.createStatement(); 
//				 ResultSet resultSet = stm.executeQuery("Select * from [QuanLyMuaBanHangHoa].[dbo].[HANG_HOA]"); 
//				 while (resultSet.next())
//				 {
//					 TableItem item = new TableItem(table, SWT.NONE); 
//					 item.setText(new String[] { resultSet.getString(1), 
//					 resultSet.getString(2), 
//					 resultSet.getString(3), 
//					 resultSet.getString(4), 
//					 resultSet.getString(5)				 
//				 });
//				 }
//				 con.close();
//				 }
//				 catch (SQLException e) {
//				// TODO Auto-generated catch block
//				e.printStackTrace();
//			}	
//			}
//		});
//				table.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
				
				TableColumn tblclmnNewColumn = new TableColumn(MHC_Bang, SWT.CENTER);
				tblclmnNewColumn.setWidth(93);
				tblclmnNewColumn.setText("M\u00E3 H\u00E0ng H\u00F3a");
				
				TableColumn clTenHangHoa = new TableColumn(MHC_Bang, SWT.CENTER);
				clTenHangHoa.setWidth(202);
				clTenHangHoa.setText("T\u00EAn H\u00E0ng H\u00F3a");
				
				
				TableColumn tblclmnNewColumn_2 = new TableColumn(MHC_Bang, SWT.CENTER);
				tblclmnNewColumn_2.setWidth(121);
				tblclmnNewColumn_2.setText("\u0110\u01A1n V\u1ECB T\u00EDnh");
				
				TableColumn tblclmnNewColumn_3 = new TableColumn(MHC_Bang, SWT.CENTER);
				tblclmnNewColumn_3.setWidth(108);
				tblclmnNewColumn_3.setText("S\u1ED1 L\u01B0\u1EE3ng T\u1ED3n");
				
				TableColumn tblclmnNewColumn_4 = new TableColumn(MHC_Bang, SWT.CENTER);
				tblclmnNewColumn_4.setWidth(150);
				tblclmnNewColumn_4.setText("\u0110\u01A1n Gi\u00E1 \u0110\u1EC1 Ngh\u1ECB");

	}

	public void capnhapTable() {
		MHC_Bang.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		MHC_Bang.setItemCount(0);
		TaoKetNoi s = new TaoKetNoi();
		Connection con = s.TaoLienKet();
		try {
			Statement stm = con.createStatement();
			ResultSet resultSet = stm
					.executeQuery("Select * from [QuanLyMuaBanHangHoa].[dbo].[HANG_HOA]");
			while (resultSet.next()) {
				TableItem item = new TableItem(MHC_Bang, SWT.NONE);
				item.setText(new String[] { resultSet.getString(1),
						resultSet.getString(2), resultSet.getString(3),
						resultSet.getString(4), resultSet.getString(5) });
			}
			con.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
